package com.scala.learn.sparkUDF

/**
  * @Copyright: Shanghai Definesys Company.All rights reserved.
  * @Description:
  * @author: chuhaitao
  * @since: 2019/3/16 21:26
  * @history:
  *          1.2019/3/16 created by chuhaitao
  */
object BinarySearch {


  def binarySearch(lists: Array[(Long, Long, String)], id: Long): Int = {

    var low = 0
    var hight = lists.length - 1

    while (low <= hight) {
      val middle = (low + hight) / 2

      if (id >= lists(middle)._1 && id <= lists(middle)._2) {
        return middle
      }
      if (id < lists(middle)._1)
        hight = middle - 1
      else
        low = middle + 1
    }
    -1
  }

}
